import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/components/msgList.dart';
import 'package:flutter_app/mock/jsonSelf.dart';
import 'package:flutter_app/provider/drawerprovider.dart';
import 'package:flutter_app/util/catch.dart';
import 'package:provider/provider.dart';
import 'dart:convert' as convert;
class HomePage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _homeState();
  }

}
class _homeState extends State{
  CachService cachService = CachService();
  var tabTitle = [
    '最新',
    '推荐'
  ];

  List tList = JsonSelf.tCardList;
  List zList = JsonSelf.zCardList;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return DefaultTabController(
      length: tabTitle.length,
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: _sliverBuilder,
          body:  TabBarView(
            children: tabTitle
                .map((s) => ListView.builder(
              itemBuilder: (context, int){
                if(s == '最新'){
                  if(zList.length == 0){
                    return Center(
                      child: Text('暂无信息'),
                    );
                  }else{
                    return Container(
                      margin: EdgeInsetsDirectional.fromSTEB(0,10,0,0),
                      padding: EdgeInsetsDirectional.fromSTEB(20,10,20,10),
                      color: Colors.white,
                      child: MsgList(zList[int]['title'],zList[int]['src'],zList[int]['name'],zList[int]['taglist'],zList[int]['content'],zList[int]['isImg'],zList[int]['cImg']),
                    );
                  }
                }
                if(s == '推荐'){
                  if(tList.length == 0){
                    return Center(
                      child: Text('暂无信息'),
                    );
                  }else{
                    return Container(
                        margin: EdgeInsetsDirectional.fromSTEB(0,10,0,0),
                        padding: EdgeInsetsDirectional.fromSTEB(20,10,20,10),
                        color: Colors.white,
                        child: MsgList(tList[int]['title'],tList[int]['src'],tList[int]['name'],tList[int]['taglist'],tList[int]['content'],tList[int]['isImg'],tList[int]['cImg'])
                    );
                  }

                }
                return Center(
                  child: Text('暂无信息'),
                );
              },
              itemCount: s == '最新'?zList.length:s == '推荐'?tList.length:1,
            ))
                .toList(),
          )
        ),
      )
    );
  }

  List<Widget> _sliverBuilder(context,bool){
    return [
      SliverAppBar(
        snap: true,
        pinned: true,
        floating: true,
        expandedHeight: 300.0,
        backgroundColor: Colors.white,
        flexibleSpace: Row(
          children: <Widget>[
            Expanded(
              child: Image.network('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1576673000494&di=63d7d62eee034a83cfcadc336c0a0796&imgtype=0&src=http%3A%2F%2Fd.paper.i4.cn%2Fmax%2F2016%2F12%2F30%2F14%2F1483077766218_556214.jpeg', fit: BoxFit.cover),
            )
          ],
        ),
        leading:
        IconButton(
          icon: const Icon(Icons.menu),
          color: Colors.white,
          onPressed: () {
            var con = Provider.of<DrawerProvider>(context).currentcontext;
            Provider.of<DrawerProvider>(con).currentScaffoldKey.currentState.openDrawer();
          },
        ),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.message),
            color: Colors.white,
            onPressed: () {
              Navigator.pushNamed(context, '/message');
            },
          ),
          IconButton(
            icon: const Icon(Icons.search),
            color: Colors.white,
            onPressed: () {
              Navigator.pushNamed(context, '/search');
            },
          ),
        ],
        bottom:TabBar(
          tabs: tabTitle.map((f) => Tab(text: f)).toList(),
          indicatorColor: Colors.lightBlue,
          unselectedLabelColor: Colors.white,
          labelColor: Colors.lightBlue,
        ),

      ),
    ];
  }
}
class SliverTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar widget;
  final Color color;

  const SliverTabBarDelegate(this.widget, {this.color})
      : assert(widget != null);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new Container(
      child: widget,
      color: color,
    );
  }

  @override
  bool shouldRebuild(SliverTabBarDelegate oldDelegate) {
    return false;
  }

  @override
  double get maxExtent => widget.preferredSize.height;

  @override
  double get minExtent => widget.preferredSize.height;
}